/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package portal.system.inzerat;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Riso
 */
@WebServlet(name = "VypisInzerat", urlPatterns = {"/inzerat"})
public class VypisInzerat extends HttpServlet {

    private String driver = portal.constants.Database.DATABASE_DRIVER;
    private Connection con = null;
    private Statement stmt = null;
    private ResultSet rs = null;
    private String userName = portal.constants.Database.DATABASE_USER;
    private String password = portal.constants.Database.DATABASE_PASSWORD;
    private String URL = portal.constants.Database.DATABASE_URL;
    private HttpSession session;
    private PrintWriter out;
    private Inzerat i;
    private ArrayList obrazky;

    @Override
    public void init() {
        try {
            super.init();
            Class.forName(driver);
            con = DriverManager.getConnection(URL, userName, password);
        } catch (Exception ex) {
        }
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        out = response.getWriter();
        try {
            nacitajInzerat(request);
            obrazky = nacitajObrazky(request);
            vypisInzerat();
        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void vypisCSS() {
        out.println();
    }

    private void nacitajInzerat(HttpServletRequest request) {
        try {
            stmt = con.createStatement();
            String sql;
            sql = "select * from inzerat where id_inzeratu=" + request.getParameter("c");
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                i = new Inzerat();
                i.setCena(rs.getInt("cena"));
                i.setDatum_pridania(rs.getTimestamp("datum_pridania"));
                i.setEmail(rs.getString("email"));
                i.setHeslo(rs.getString("heslo"));
                i.setId_inzeratu(rs.getInt("id_inzeratu"));
                i.setId_usera(rs.getInt("id_usera"));
                i.setMeno(rs.getString("meno"));
                i.setNazov_inzeratu(rs.getString("nazov_inzeratu"));
                i.setText_inzeratu(rs.getString("text_inzeratu"));
                i.setId_podkategorie(rs.getInt("id_podkategorie"));
                i.setPocet_zobrazeni(rs.getInt("pocet_zobrazeni"));
            }
            stmt.executeUpdate("update inzerat set pocet_zobrazeni = pocet_zobrazeni+1 where id_inzeratu = "+i.getId_inzeratu());
            rs.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ArrayList nacitajObrazky(HttpServletRequest request) {
        ArrayList obr = new ArrayList();
        try {
            stmt = con.createStatement();
            String sql;
            sql = "select id_obrazku from obrazky_inzerat where id_inzeratu=" + request.getParameter("c");
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                obr.add(rs.getString("id_obrazku"));
            }
            rs.close();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obr;
    }

    private void vypisInzerat() {
        out.println(i.getNazov_inzeratu()+" - "+i.getDatum_pridania());
        out.println("<br/>");
        out.println(i.getText_inzeratu());
        out.println("<br/>");        
        out.println("Cena: "+i.getCena());
        out.println("<br/>");
        for (int j = 0; j < obrazky.size(); j++) {
            out.println("<img class=\"obrazok\" id=\"obrazok\" src=\"./displayImage?obr="+obrazky.get(j) +"\" /><br/>");
        }
        out.println();
        out.println("vypisujem inzerat a všetky blbosti ktoré by tam mali byť");
    }
}
